#include "ShareMem.hpp"
#include <stdexcept>
#include <unistd.h>
#include <cstring>

ShareMem::ShareMem(const char* name,unsigned long size)
  : mem_size(size)
{
  fd=shm_open(name,O_CREAT | O_RDWR,S_IRUSR | S_IWUSR);
  if(fd==-1)
    throw std::runtime_error("Can't open share mem");
  if(ftruncate(fd,size))
    throw std::runtime_error("Can't get memory size");
  mem_ptr=mmap(nullptr,size,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);
  if(mem_ptr==MAP_FAILED)
    throw std::runtime_error("Can't map shared memory");
  memset(mem_ptr,0x0,size);
}

ShareMem::~ShareMem()
{}

void* ShareMem::GetMemory()
{
  return mem_ptr;
}

